Contents | Index | < Browse | Browse >
LETTERcallocULETTER
Allocate and initialize memory.
Overview
#include <stdlib.h>
b = calloc(elements, size);
void *b; // pointer to memory block
size_t elements; // number of elements
size_t size; // size of each element
Portability
ANSI
Description
This function equals "malloc" but allocates memory for an array consisting
of "elements" elements which are "size" bytes big. The memory block will
be initialized with zeros.
Returns
This function returns zero if not enough memory could be allocated and
a pointer to the memory block otherwise.
See also
free , malloc , realloc
Example
#include <stdio.h>
#include <string.h>
void main(void)
{
char *str = NULL;
if(str = (char *)calloc(10, sizeof(char)))
{
strcpy(str, "StormC");
printf("Memory block's contents: %s\\n", str);
free(str);
}
else
printf("Not enough memory\\n");
}